﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Wu.MyBlog.Repository.Base;
using Wu.MyBlog.Model.IServices.Base;

namespace Wu.MyBlog.Model.Services.Base
{
    public class BaseServices<T> : IBaseServices<T> where T : class
    {
        public IBaseRepository<T> baseDal;

        #region 增加
        public void Add(T model)
        {
            baseDal.Add(model);
        }
        #endregion

        #region 删除
        public void Delete(T model, bool isadded)
        {
            baseDal.Delete(model, isadded);
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 传入模型修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="propertys"></param>
        public void Edit(T model, string[] propertys)
        {
            baseDal.Edit(model, propertys);
        }
        
        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="model"></param>
        public void Edit(T model)
        {
            baseDal.Edit(model);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 降序还是升序分页查询
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pagesize"></param>
        /// <param name="rowcount"></param>
        /// <param name="predicate"></param>
        /// <param name="keySelector"></param>
        /// <param name="IsQueryOrderBy"></param>
        /// <returns></returns>
        public List<T> QueryByPage<TKey>(int pageIndex, int pagesize, out int rowcount, Expression<Func<T, bool>> 
            predicate, Expression<Func<T, TKey>> keySelector, bool IsQueryOrderBy)
        {
            return baseDal.QueryByPage(pageIndex, pagesize, out rowcount,predicate,keySelector,IsQueryOrderBy);
        }
        /// <summary>
        /// 多表关联查询
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="tableNames"></param>
        /// <returns></returns>
        public List<T> QueryJoin(Expression<Func<T, bool>> predicate, string[] tableNames)
        {
            return baseDal.QuertJion(predicate, tableNames);
        }

        /// <summary>
        /// 升序还是降序查询
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="keySelector"></param>
        /// <param name="IsQueryOrderBy"></param>
        /// <returns></returns>
        public List<T> QueryOrderBy<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>>
            keySelector, bool IsQueryOrderBy)
        {
            return baseDal.QuertOrderBy(predicate,keySelector,IsQueryOrderBy);
        }
        /// <summary>
        /// 单表查询
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public List<T> QueryWhere(Expression<Func<T, bool>> predicate)
        {
            return baseDal.QueryWhere(predicate);
        }
        #endregion

        #region 调用存储过程返回一个指定的TResult
 
        public List<T> RunProc<T>(string sql, params object[] pamrs)
        {
            return baseDal.RunProc<T>(sql, pamrs);
        }
        #endregion

        #region 统一提交
        public int SaverChanges()
        {
            return baseDal.SaverChanges();
        }
        #endregion
    }
}
